var pageii = null;
var grid = null;
$(function () {
    grid = lyGrid({
        id: 'paging',
        l_column: [{
            colkey: "id",
            name: "id",
            width: "50px",
            hide: true
        }, {
            colkey: "channels",
            name: "所属通道",
            renderData: function (rowindex, data, rowdata, column) {
                if (!data || data == '') {
                    return '全部';
                } else if(data=="deshigu_wechat") {
                    return "微信小程序";
                }else if(data=="deshigu_app"){
                    return "APP";
                }else{
                    return "";
                }
            }
        }, {
            colkey: "goodName",
            name: "商品名称"
        },{
            colkey: "name",
            name: "规格名称"
        }, {
            colkey: "price",
            name: "商品价格"
        }, {
            colkey: "salecount",
            name: "商品销量"
        }, {
            colkey: "stock",
            name: "商品库存"
        }, {
            colkey: "yprice",
            name: "商品原价"
        },  {
            colkey: "status",
            name: "状态",
            width: "100px",
            renderData: function (rowindex, data, rowdata, column) {
                var str = "";
                if (data == "0") {
                    str = "正常";
                } else if (data == "1") {
                    str = "<span style='color: green;'>下架</span>";
                }
                return str;
            }
        }],
        jsonUrl: rootPath + '/store/product/findByPage.shtml',
        dymCol: true,
        checkbox: true
    });

    $("#search").click("click", function () {// 绑定查询按扭
        var searchParams = $("#searchForm").serializeJson();// 初始化传参数
        grid.setOptions({
            data: searchParams
        });
    });

    $("#addFun").click("click", function () {
        addFun();
    });
    $("#editFun").click("click", function () {
        editFun();
    });
    $("#delFun").click("click", function () {
        delFun();
    });
});

function editFun() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("只能选中一个");
        return;
    }
    pageii = layer.open({
        title: "编辑",
        type: 2,
        area: ["800px", "60%"],
        content: rootPath + '/store/product/editUI.shtml?id=' + cbox
    });
}

function addFun() {
    pageii = layer.open({
        title: "新增",
        type: 2,
        area: ["800px", "60%"],
        content: rootPath + '/store/product/addUI.shtml'
    });
}

function delFun() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {
        layer.msg("请选择删除项！！");
        return;
    }
    layer.confirm('是否删除？', function (index) {
        var url = rootPath + '/store/product/deleteEntity.shtml';
        var s = CommnUtil.ajax(url, {
            ids: cbox.join(",")
        }, "json");
        if (s == "success") {
            layer.msg('删除成功');
            grid.loadData();
        } else {
            layer.msg('删除失败');
        }
    });
}
